import {
  Controller,
  Get,
  Post,
} from '../decorator/router'

import Pug from 'pug'
import { pugTpl, videoTpl } from '../tpl'
import { getCourses } from '../service/courses'
import { saveComment, getComments } from '../service/comments'
import { saveQuestion, getQuestions } from '../service/questions'

@Controller('/learn')
export default class DocumentRouter {
  @Post('/comment')
  async submitComment (ctx, next) {
    const {courseid, tid, content, commentid} = ctx.request.body
    console.log(ctx.session)
    console.log(content)
    if (!ctx.session.user) {
      return (ctx.body = {
        code: 1,
        msg: '请先登录'
      })
    }

    let comment = await saveComment({
      email: ctx.session.user.email,
      commentid,
      tid,
      courseid, 
      content
    })
    console.log(comment)
    ctx.body = {
      code: 0,
      comment: comment,
      msg: '评论成功'
    }
  }

  @Get('/:courseid/comment')
  async getComment (ctx, next) {
    const courseid = ctx.params.courseid
    let ip = ctx.request.headers['x-real-ip'] || 
             ctx.request.headers['x-forwarded-for'] ||
             ctx.request.socket.remoteAddress || '';


    console.log(courseid)
    console.log('ctx.query')
    console.log(ctx.query)
    const page = ctx.query.page || 1
    let {data, count} = await getComments({courseid, page})

    ctx.body = {
      code: 0,
      data: data,
      ip: ip,
      count,
      msg: '获取评论列表成功'
    }
  }

  @Get('/:courseid/list')
  async getCourse (ctx, next) {
    console.log(ctx.params)
    const courseid = ctx.params.courseid
    let course = await getCourses(courseid)
    ctx.body = {
      ...course,
      courseid
    }
  }

  @Get('/:courseid/chepter/:chepterid')
  async getDetail (ctx, next) {
    const courseid = ctx.params.courseid
    const chepterid = ctx.params.chepterid
    let course = await getCourses(courseid, chepterid)
    ctx.body = Pug.render(pugTpl, {
      courseid,
      chepterid,
      pages: course.pages,
      nav: course.nav,
      body: course.body
    })
  }

  @Get('/video/:videoid')
  async getVideo (ctx, next) {
    console.log(ctx.params)
    const courseid = ctx.params.videoid
    // let course = await getCourses(courseid)
    const urlArr = [
      'http://v.bestqd.cn/4c72da35699946df98c9454dee1b1f0c/dd85c791050b435b97bc846071750c33-54079045c496a39c056074c25e3781ad-ld.m3u8?auth_key=1526654250-1565a7c605d54621ac4d5c23b3f5f2f6-0-2bc58b3056b99426c817a361b40634df',
      'http://v.bestqd.cn/75c246bf0ee344c3beac261a1994fe0c/57ae27a4034f475bb8ee73865245db5f-8621c927e9b7c2254f86cda263ae3ef2-ld.m3u8?auth_key=1526654277-4db2e717426e41a592dd71e297434b71-0-02552ba24887d1320bec53623cfae983',
      'http://v.bestqd.cn/0d381d46d9dd499d9d83980597c9647c/425e0001ad8d44d6b7b89be94de47d1c-cf3dffa3681d0071de2326de66a69b6e-ld.m3u8?auth_key=1526654303-a24d3fe73eb648809bcda92abde8d333-0-621ffb80e8dbcdccaae5bfa75d8a4d09',
      'http://v.bestqd.cn/cee31c0cd82843f1abdfb60fb0674d1e/69dc2ab28dd0401ca291e5364bd7065d-b9a9fc0917f4fb7d7e3e43b8d7b20cab-ld.m3u8?auth_key=1526654512-4bf95fc9008b4d58aa5658de599fd6a9-0-c0b98162322f9769012ba82a960d0591',
      'http://v.bestqd.cn/ff8a7e025d424171b0dd0e1a09c13036/ba03be17830e4c09a74f0a3d39cfd000-65d90a6455f3e8ae80bd701931946aa6-ld.m3u8?auth_key=1526654530-bd6d2a3c90ac4aed836300f4c107f6f5-0-b9d7fa58f742fc9bfabac08e63ed6c98',
      'http://v.bestqd.cn/5e21c06e22264ab6ae86c3ebf65898a8/2cbdc2f292ee45a7ae5e5b13ae569fdc-bd0462c4455ddf1108ccb57ceca33850-ld.m3u8?auth_key=1526654552-ec7f413c5ed34681b56eeb835f0e57ce-0-e105b165374b2ee95ba2f81891f7b71d',
      'http://v.bestqd.cn/5acf5ba4ad474c9d83e205fb0be5590a/efc9e3adffec44dfbd43c32958cf7251-1f1ec36deb402cef14d8b0895f8a0086-ld.m3u8?auth_key=1526654568-2e3eee6d9ddf4b6b86ba152c036d788f-0-b398a7306e0716d457840bde61216027',
      'http://v.bestqd.cn/38278cf8c22f4856b12f2d4e46d887d0/9c92dcd4f4314c1f9daaf155197d1376-773e60feebe6c1be20f7574a612ef076-ld.m3u8?auth_key=1526654579-b318de9951aa46088b98eee7a02b01e2-0-a8a7048c8691363199a296400c174a38'
    ]

    let video_src = ''

    switch (courseid) {
      case '11011':
        video_src = urlArr[0]
        break
      case '11012':
        video_src = urlArr[1]
        break
      case '11021':
        video_src = urlArr[2]
        break
      case '11022':
        video_src = urlArr[3]
        break
      case '11023':
        video_src = urlArr[4]
        break
      case '11024':
        video_src = urlArr[5]
        break
      case '11031':
        video_src = urlArr[6]
        break
      case '11032':
        video_src = urlArr[7]
        break
    }

    const video_list = 
      [
        {
          chapter: "准备", 
          list:[
            {title: '演示：公众号支付', url: '/video/11011'},
            {title: '准备项目', url: '/video/11012'}
          ]
        },
        {
          chapter: '授权', 
          list:[
            {
              title: '配置网页授权目录与支付授权目录', 
              url: '/video/11021'
            }, 
            {
              title: 'OpenID 与 UnionID', 
              url: '/video/11022'
            },
            {
              title: '微信网页授权：获取 code 值', 
              url: '/video/11023'
            },
            {
              title: '微信网页授权：获取 access_token，得到用户的 openid', 
              url: '/video/11024'
            }
          ]
        },
        {
          chapter: '公众号支付', 
          list:[
            {
              title: '公众号支付：统一下单', 
              url: '/video/11031'
            }, 
            {
              title: 'JSAPI：准备参数与支付', 
              url: '/video/11032'
            }
          ]
        }
      ]

    ctx.body = Pug.render(videoTpl, {
      video_list: video_list,
      video_src: video_src
    })
  }

}
